package com.tianqu.tms.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TransferSettlementDateDao;
import com.tianqu.tms.dto.TransferSettlementDateDto;

public class TransferSettlementDate extends TmsEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3444267478879920603L;

	private int id;
	private String startDate;// 结算开始日期
	private String endDate;// 结算截至日期
	private int insertUserId;// 结算受理人
	private int settleState;// 结算状态
	private int settleDayId;
	
	public TransferSettlementDate getDateInfo(int type,int insertUserId) throws TmsDaoException
	{
		TransferSettlementDateDao uDao = (TransferSettlementDateDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_SETTLEMENT_DATE);	
		TransferSettlementDateDto dto = uDao.getDateInfo(type,insertUserId);
		TransferSettlementDate entity = null;
		if(dto!=null)
		{
			//将Dto信息set到entity中
			entity = new TransferSettlementDate();
			entity.setId(dto.getId());
			entity.setStartDate(DateUtil.getDateByTimeStampHM(dto.getStartDate()));
			entity.setEndDate(DateUtil.getDateByTimeStampHM(dto.getEndDate()));
			entity.setInsertUserId(dto.getInsertUserId());
			entity.setSettleState(dto.getSettleState());
		}
		return entity;
	}
	public void updateSettleState(int insertUserId) throws TmsDaoException
	{
		TransferSettlementDateDao uDao = (TransferSettlementDateDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_SETTLEMENT_DATE);	
		uDao.updateSettleState(insertUserId);
	}

	public void updateSettleStateOne(TransferSettlementDate entity)throws TmsDaoException
	{
		TransferSettlementDateDao uDao = (TransferSettlementDateDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_SETTLEMENT_DATE);	
		TransferSettlementDateDto dto = new TransferSettlementDateDto();
		dto.setEndDate(DateUtil.getStringToTimeStamp1(entity.getEndDate()+":00"));
		dto.setId(entity.getId());
		dto.setSettleState(entity.getSettleState());
		dto.setSettleDayId(entity.getSettleDayId());
		uDao.updateSettleStateOne(dto);
	}
	public void insertTransferSettlement(TransferSettlementDate entity)throws TmsDaoException
	{
		TransferSettlementDateDao uDao = (TransferSettlementDateDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_SETTLEMENT_DATE);	
		TransferSettlementDateDto dto = new TransferSettlementDateDto();
		dto.setStartDate(DateUtil.getStringToTimeStamp1(entity.getStartDate()+":00"));
		dto.setInsertUserId(entity.getInsertUserId());
		dto.setSettleState(entity.getSettleState());
		uDao.insertTransferSettlement(dto);
	}
	public List getSettleDateInfo(int type,int insertUserId)throws TmsDaoException
	{
		TransferSettlementDateDao uDao = (TransferSettlementDateDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_SETTLEMENT_DATE);	
		List<TransferSettlementDateDto> list = uDao.getSettleDateInfo(type,insertUserId);
		List dateList = null;
		if(list!=null)
		{
			dateList = new ArrayList();
			for(int i=0;i<list.size();i++)
			{
				TransferSettlementDateDto dto = (TransferSettlementDateDto)list.get(i);
				Map map = new HashMap();
				map.put("settlePeriodName", DateUtil.getDateByTimeStampHM(dto.getStartDate())+" - "+DateUtil.getDateByTimeStampHM(dto.getEndDate()));
				map.put("settlePeriodId", dto.getId());
				dateList.add(map);
			}
		}
		return dateList;
	}
	public String getSettleDateIdBySettleDayId(String settleDayId)throws TmsDaoException
	{
		TransferSettlementDateDao uDao = (TransferSettlementDateDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_SETTLEMENT_DATE);	
		List<TransferSettlementDateDto> list = uDao.getSettleDateIdBySettleDayId(Integer.parseInt(settleDayId));
		String str = "";
		if(list!=null)
		{
			for(int i=0;i<list.size();i++)
			{
				TransferSettlementDateDto dto = (TransferSettlementDateDto)list.get(i);
				str+=dto.getId()+",";
			}
			str = str.substring(0, str.length()-1); 
		}
		return str;
	}
	public TransferSettlementDate getDateInfoBySettleId(String settleId,int userId)throws TmsDaoException
	{
		TransferSettlementDateDao uDao = (TransferSettlementDateDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_SETTLEMENT_DATE);	
		TransferSettlementDateDto dto = uDao.getDateInfoBySettleId(settleId,userId);
		TransferSettlementDate entity = null;
		if(dto!=null)
		{
			//将Dto信息set到entity中
			entity = new TransferSettlementDate();
			entity.setId(dto.getId());
			entity.setStartDate(DateUtil.getDateByTimeStampHM(dto.getStartDate()));
			entity.setEndDate(DateUtil.getDateByTimeStampHM(dto.getEndDate()));
			entity.setInsertUserId(dto.getInsertUserId());
			entity.setSettleState(dto.getSettleState());
		}
		return entity;
	}
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public int getInsertUserId() {
		return insertUserId;
	}

	public void setInsertUserId(int insertUserId) {
		this.insertUserId = insertUserId;
	}

	public int getSettleState() {
		return settleState;
	}

	public void setSettleState(int settleState) {
		this.settleState = settleState;
	}

	public int getSettleDayId() {
		return settleDayId;
	}
	public void setSettleDayId(int settleDayId) {
		this.settleDayId = settleDayId;
	}
	@Override
	public void save() throws TmsDaoException, TmsException {
		// TODO Auto-generated method stub

	}

}
